package com.aruba.databinding;

import android.text.TextUtils;
import android.widget.ImageView;

import androidx.databinding.Bindable;
import androidx.databinding.BindingAdapter;

import com.squareup.picasso.Picasso;

/**
 * Created by aruba on 2021/9/11.
 */
public class ImageViewBindingAdapter {
    @BindingAdapter("image")
    public static void setImage(ImageView imageView, String url) {
        if (!TextUtils.isEmpty(url)) {
            Picasso.get()
                    .load(url)
                    .placeholder(R.drawable.sijiali)
                    .into(imageView);
        } else {
            imageView.setImageResource(R.drawable.sijiali);
        }
    }

    @BindingAdapter("image")
    public static void setImage(ImageView imageView, int resId) {
        imageView.setImageResource(resId);
    }

    @BindingAdapter(value = {"image", "default"}, requireAll = false)
    public static void setImage(ImageView imageView, String url, int resId) {
        if (!TextUtils.isEmpty(url)) {
            Picasso.get()
                    .load(url)
                    .placeholder(resId)
                    .into(imageView);
        } else {
            imageView.setImageResource(resId);
        }
    }
}
